window.DeviceManagement = (function($, module) {	
	/**
	 * 初始化函数
	 */
	function init(){
		initPage(1);
	}
	
	//默认加载  
    function initPage(pageNo){
    	var paramData = CommonUtils.serializeToJSON($(".select_text input").serializeArray());
    	$.ajax({
            url:"deviceManagement/getDevices/"+pageNo+"/10",
            type:"post",
            data:JSON.stringify(paramData),
            contentType:"application/json;charset=utf-8",
            success:function(data){
            	showBills(data);
        		$(".pager").Page({
        			totalPages: data.totalPage||0,//分页总数
        			liNums: 7,//分页的数字按钮数(建议取奇数)
        			activeClass: 'activP', //active 类样式定义
        			callBack : function(pageNo){
        				PageClick(pageNo);
        			}
        		}); 

            },
            error:function(data){
                alert("error");
            }
        });
    }
    
    //回调函数  
    function PageClick(pageNo) {  
        initPage(pageNo); 
    }
	
    $(".search").click(function(){
    	initPage(1);
    });
	//列表展示
	function showBills(data){
		$("#tableList").html("");
    	var datas = data['deviceList'];
    	if(datas){
    		$.each(datas,function(item,obj){
    			var dom = "";
    			dom += "<tr class='listTableText list_list'>";
    			dom += "<td><input type='checkBox' class='input_checkBox' id='"+CommonUtils.formatStr(obj.ipAddress)+"'></td>";
    			dom += "<td><a class='cursor' onclick='DeviceManagement.toUpdateDeviceJsp(\""+CommonUtils.formatStr(obj.ipAddress)+"\")'>"+CommonUtils.formatStr(obj.deviceName)+"</a></td>";
    			dom += "<td>"+CommonUtils.formatStr(obj.storeName)+"</td>";
    			dom += "<td>"+(CommonUtils.formatStr(obj.type)==1?'入库':'出库')+"</td>";
    			dom += "<td>"+CommonUtils.formatStr(obj.ipAddress)+"</td>";
    			dom += "<td>"+CommonUtils.formatStr(obj.port)+"</td>";
    			dom += "</tr>";			
    			$("#tableList").append(dom);
    		});
    		$("tbody tr:odd").css("background","#EBF5FF");  	
    	}
    }
	
	function toAddDeviceJsp(){
		window.location.href = basePath + "deviceManagement/toAddDeviceJsp";
	}
	
	function toUpdateDeviceJsp(ipAddress){
		window.location.href = basePath + "deviceManagement/toUpdateDeviceJsp?ipAddress="+ipAddress;
	}
	
	/**
     * 删除物料
     */
    function deleteDevices(){
        var list = [];//项目中要删除的id集合
        var checks = $("input[type='checkbox']:checked");
        $.each(checks,function(index,obj){
            list.push(obj.id);
        });
        
        if (list.length == 0){
            return;
        }
        
        window.confirm("确定要删除吗?",function(result){
            if (result){
                var deleteMaterialsUrl = 'deviceManagement/deleteDevices';
                $.ajax({
                    url : deleteMaterialsUrl,
                    data : {ipAddress:list},
                    type : 'POST',
                    traditional: true,//这里设置为true
                	success:function() {
                		alert("删除成功");
                		initPage(1);
                	},
                	error:function(){
	                    alert("删除失败");
	                }
                });
            }
        });
    }
	
	module.init = init;
	module.toAddDeviceJsp = toAddDeviceJsp;
	module.toUpdateDeviceJsp = toUpdateDeviceJsp;
	module.deleteDevices = deleteDevices;
	return module;
}($, window.DeviceManagement || {}));
$(function() {
	DeviceManagement.init();
});